﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace UI
{
    public class SkillSetting : Page
    {
        public UILabel skillNameLeft;
        public UILabel skillNameRight;
        public GameObject skillIconLeft;
        public GameObject skillIconRight;

        string heroGuid;
        bool leftDraging = false;
        bool rightDraging = false;
        string leftSkillId = "";
        string rightSkillId = "";
        Vector3 leftPos;
        Vector3 rightPos;
        Vector3 mousePos;

        void Start()
        {
            leftPos = skillIconLeft.transform.localPosition;
            rightPos = skillIconRight.transform.localPosition;
        }

        public void OnExchangeBtn()
        {
            skillIconLeft.transform.localPosition = rightPos;
            skillIconRight.transform.localPosition = leftPos;

            leftPos = skillIconLeft.transform.localPosition;
            rightPos = skillIconRight.transform.localPosition;

            string skillid = leftSkillId;
            leftSkillId = rightSkillId;
            rightSkillId = skillid;

            skillNameLeft.text = TableTextBlo.GetText("name_skill_" + leftSkillId);
            skillNameRight.text = TableTextBlo.GetText("name_skill_" + rightSkillId);
        }

        public void OnConfirmBtn()
        {
            UIHero hero = StaticData.playerData.GetUIHero(heroGuid);
            for (int i = 0; i < hero.Skills.Length; i++)
            {
                if (hero.Skills[i].skillIndex == 0 && hero.Skills[i].skillData.SkillId == leftSkillId)
                {
                    Close();
                    return;
                }
            }

            CSSetSkillOrder msg = new CSSetSkillOrder();
            msg.heroGuid = heroGuid;
            msg.skillOrder = leftSkillId + "," + rightSkillId;
            NetAPI.SendHttp(OpDefine.CSSetSkillOrder, msg, MsgCallback);
        }

        void MsgCallback(NetWork.Packets.Packet packet,bool bsuccess)
        {
            if(bsuccess)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("skill_set_success"));

                UIHero hero = StaticData.playerData.GetUIHero(heroGuid);
                hero.UpdateSkillIndex(leftSkillId, 0);
                hero.UpdateSkillIndex(rightSkillId, 1);

                Close();
            }
        }

        public void OnSkillLeftDragStart()
        {
            skillIconLeft.GetComponent<UIWidget>().depth = skillIconRight.GetComponent<UIWidget>().depth + 1;
            mousePos = Input.mousePosition;
            leftDraging = true;
        }

        public void OnSkillLeftDragEnd()
        {
            leftDraging = false;
            Vector3 pos = skillIconLeft.transform.localPosition;
            if (Mathf.Abs(pos.x - rightPos.x) <= 70)
            {
                OnExchangeBtn();
            }
            else
            {
                skillIconLeft.transform.localPosition = leftPos;
                skillIconRight.transform.localPosition = rightPos;
            }
        }

        public void OnSkillRightDragStart()
        {
            skillIconRight.GetComponent<UIWidget>().depth = skillIconLeft.GetComponent<UIWidget>().depth + 1;
            mousePos = Input.mousePosition;
            rightDraging = true;
        }

        public void OnSkillRightDragEnd()
        {
            rightDraging = false;
            Vector3 pos = skillIconRight.transform.localPosition;
            if (Mathf.Abs(pos.x - leftPos.x) <= 70)
            {
                OnExchangeBtn();
            }
            else
            {
                skillIconLeft.transform.localPosition = leftPos;
                skillIconRight.transform.localPosition = rightPos;
            }
        }

        void Update()
        {
            if (leftDraging)
            {
                Vector3 v = Input.mousePosition;
                skillIconLeft.transform.localPosition = new Vector3(leftPos.x + (v.x - mousePos.x),leftPos.y + (v.y - mousePos.y),leftPos.z);
            }
            if (rightDraging)
            {
                Vector3 v = Input.mousePosition;
                skillIconRight.transform.localPosition = new Vector3(rightPos.x + (v.x - mousePos.x), rightPos.y + (v.y - mousePos.y), rightPos.z);
            }
        }

        void UpdateSkills()
        {
            UIHero hero = StaticData.playerData.GetUIHero(heroGuid);
            for(int i = 0; i < hero.Skills.Length; i++)
            {
                if(hero.Skills[i].skillIndex == 0)
                {
                    leftSkillId = hero.Skills[i].skillData.SkillId;
                    skillNameLeft.text = TableTextBlo.GetText("name_skill_" + leftSkillId);
                    skillIconLeft.GetComponent<TextureKit>().textureName = hero.Skills[i].skillData.SkillIcon;
                }
                else if(hero.Skills[i].skillIndex == 1)
                {
                    rightSkillId = hero.Skills[i].skillData.SkillId;
                    skillNameRight.text = TableTextBlo.GetText("name_skill_" + rightSkillId);
                    skillIconRight.GetComponent<TextureKit>().textureName = hero.Skills[i].skillData.SkillIcon;
                }
            }
        }

        protected override void DoOpen()
        {
            GetComponent<PageTweener>().Play();

            heroGuid = options["guid"];
            UpdateSkills();
            base.DoOpen();
        }

        protected override void DoClose()
        {
            base.DoClose();
        }
    }
}
